/**
 * Created by wuxue on 2017/7/26.
 */
$(function () {
    $("#goodsList").gridTable({
        url: "innerGoods/pageGoods",
        columns: [
            {title: "商品图片", field: "goodsImg", format: toImg},
            {title: "商品名称", field: "goodsName"},
            {title: "基准价", field: "goodsMarketPrice", dataType:"decimal"},
            {title: "供应价", field: "goodsSellPrice", dataType: "decimal"},
            {title: "结算价", field: "goodsSettlementPrice", dataType: "decimal"},
            {title: "单位", field: "goodsUnit"},
            {title: "规格", field: "goodsSpec"},
            {title: "箱规", field: "boxSpec"},
            {title: "条码", field: "goodsBarCode"},
            {title: "状态", field: "goodsStatus",format:toStatus },
            {title: "商品类型", field: "goodsTypeName"},
            {title: "供应商", field: "orgName"},
            {title: "创建时间", field: "createTime"}
        ],
        pageSize: 10,
        idField: "id",
        infoUrl: "goods/innerGoodsInfo.html",
        controls:{
            edit:{
                show:false
            },
            del:{
                show:false
            }
        }
    });
});

function toImg(url) {
    return "<img src='"+url+"' width='40' height='40'>";
}

//判断状态
function toStatus(goodsStatus){
    switch (goodsStatus) {
        case '0':
            return "<span class='label label-default'>待申请上架</span>";
        case '5':
            return "<span class='label label-danger'>上架驳回</span>";
        case '9':
            return "<span class='label label-danger'>上架待审核</span>";
        case '10':
            return "<span class='label label-success'>上架</span>";
        case '15':
            return "<span class='label label-success'>修改待审核</span>";
        case '20':
            return "<span class='label label-success'>修改驳回</span>";
        case '30':
            return "<span class='label label-success'>下架待审核</span>";
        case '50':
            return "<span class='label label-success'>下架驳回</span>";
        case '90':
            return "<span class='label label-success'>下架</span>";
        default:
            return goodsStatus;
    }
}
//商品上架
function goodsPublish() {
    var gridTableList = $$selection.get("gridTable", "goodsList");
    var selectedRows = gridTableList.getSelectedRows();
    if (selectedRows.length === 0) {
        $.error("未选择任何数据");
        return;
    }
    selectedRows.forEach(function (row) {
        row.setStyleEdit();
    });
    $.confirm({
        title: "商品取消屏蔽上架确认",
        icon: "fa fa-question-circle-o",
        content: "<p>确认取消屏蔽该商品吗？</p>",
        theme: "modern",
        type: "orange",
        buttons: {
            ok: {
                text: "取消屏蔽商品",
                btnClass: "btn-blue",
                action: function () {
                    var arr=[];
                    selectedRows.forEach(function (row) {
                        var id = row.getData().id;
                        arr.push(id)
                    });
                    $.doAjax("innerGoods/upGoods", {idStr: arr.join(",")}, function () {
                        gridTableList.reloadData();
                        $.success("取消屏蔽成功");
                    }, function (error) {
                        $.error(error.msg);
                    });
                }
            },
            cancel: {
                text: "取消",
                btnClass: "btn-default",
                action: function () {
                    selectedRows.forEach(function (row) {
                        row.unsetStyleEdit();
                    });
                }
            }
        }
    })
}
//商品下架
function downGoods() {
    var gridTableList = $$selection.get("gridTable", "goodsList");
    var selectedRows = gridTableList.getSelectedRows();
    if (selectedRows.length === 0) {
        $.error("未选择任何数据");
        return;
    }
    selectedRows.forEach(function (row) {
        row.setStyleEdit();
    });
    $.confirm({
        title: "商品屏蔽确认",
        icon: "fa fa-question-circle-o",
        content: "<p>确认屏蔽该商品吗？</p>",
        theme: "modern",
        type: "orange",
        buttons: {
            ok: {
                text: "屏蔽商品",
                btnClass: "btn-blue",
                action: function () {
                    var arr=[];
                    selectedRows.forEach(function (row) {
                        var id = row.getData().id;
                        arr.push(id)
                    });
                    $.doAjax("innerGoods/downGoods", {idStr: arr.join(",")}, function () {
                        gridTableList.reloadData();
                        $.success("屏蔽成功");
                    }, function (error) {
                        $.error(error.msg);
                    });
                }
            },
            cancel: {
                text: "取消",
                btnClass: "btn-default",
                action: function () {
                    selectedRows.forEach(function (row) {
                        row.unsetStyleEdit();
                    });
                }
            }
        }
    })
}